<template>
  <el-dialog title="缺陷" :close-on-click-modal="false" :visible.sync="visible" :append-to-body="true"
    class="SHZY-dialog SHZY-dialog_center" lock-scroll width="600px" @close="handleClose">
    <el-form v-if="!readOnly" ref="elForm" :model="dataForm" size="small" label-width="50px" label-position="right"
      :rules="rules">
      <el-form-item label="缺陷" prop="defect">
        <el-select v-model='dataForm.defect' placeholder='请选择缺陷' filterable clearable required
          :style='{ "width": "100%" }'>
          <el-option v-for="(item, index) in defectBaseData" :key="index" :label="item.name" :value="item.id">
            <span>{{ item.name }}</span>
            <el-tag size="small" v-if="item.severity == 'tall'" type="danger" effect="dark"
              style="margin-left: 5px;">高</el-tag>
            <el-tag size="small" v-if="item.severity == 'middle'" type="warning" effect="dark"
              style="margin-left: 5px;">中</el-tag>
            <el-tag size="small" v-if="item.severity == 'low'" type="info" effect="dark"
              style="margin-left: 5px;">低</el-tag>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数量" prop="defectQty">
        <el-input v-model='dataForm.defectQty' placeholder='请输入数量' clearable required :style='{ "width": "100%" }'
          maxlength='200'>
        </el-input>
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="dataForm.remark" show-word-limit :style='{ "width": "100%" }' type='textarea'
          :autosize='{ "minRows": 4, "maxRows": 4 }' maxlength='500'>
        </el-input>
      </el-form-item>
      <el-form-item label="附件" prop="fileInfo">
        <SHZY-UploadFz v-model='dataForm.fileInfo' showTip :fileSize='5' sizeUnit='MB' :limit='1' buttonText='点击上传'>
        </SHZY-UploadFz>
      </el-form-item>
    </el-form>
    <el-timeline class="timeline">
      <el-timeline-item v-for="(item, index) in defectList" :key="index">
        <div class="el-timeline-content">
          <p class="text">
            <el-tag size="medium">{{ item.defectName }}</el-tag>
            <el-tag size="small" effect="dark" style="margin-left: 5px;">{{ item.defectQty }}</el-tag>
            <el-tag size="small" v-if="item.severity == '高'" type="danger" effect="dark" style="margin-left: 5px;">{{
              item.severity }}</el-tag>
            <el-tag size="small" v-if="item.severity == '中'" type="warning" effect="dark" style="margin-left: 5px;">{{
              item.severity }}</el-tag>
            <el-tag size="small" v-if="item.severity == '低'" type="info" effect="dark" style="margin-left: 5px;">{{
              item.severity }}</el-tag>
          </p>
          <p class="text">
            {{ item.remark }}
          </p>
          <p class="text">
            <template v-if="item.fileInfo.length">
              <ul class="el-upload-list el-upload-list el-upload-list--text">
                <li class="el-upload-list__item is-success" v-for="(file, index) in item.fileInfo" :key="file.fileId"
                  :class="{ 'el-upload-list__item_detail': true }">
                  <a class="el-upload-list__item-name" @click="handleClick(file)">
                    <i class="el-icon-paperclip"></i>{{ file.name }}
                  </a>
                </li>
              </ul>
            </template>
          </p>
          <p class="time">
            <span>{{ item.creatorUserId }} 提交于 {{ shzy.toDate(item.creatorTime, "yyyy-MM-dd HH:mm:ss") }}</span>
            <span v-if="!readOnly" class="del" @click="handleDel(index, item.id)">
              <i class="el-icon-delete"></i>删除
            </span>
          </p>
        </div>
      </el-timeline-item>
    </el-timeline>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('common.cancelButton') }}</el-button>
      <el-button :loading="btnLoading" type="primary" @click="dataFormSubmit()">确 定</el-button>
    </span>
  </el-dialog>
</template>
  
<script>
import { mapGetters } from "vuex"
import { getIpqcInspectionitemdefectList, createIpqcInspectionitemdefect, delIpqcInspectionitemdefect } from '@/api/q1/business/ipqcDefect'
import { getDownloadUrl } from '@/api/common'
export default {
  computed: {
    ...mapGetters(['userInfo'])
  },
  data() {
    return {
      visible: false,
      btnLoading: false,
      isRefresh: false,
      text: '',
      defectList: [
        // {
        //   defect: "1", defectName: "外观严重破损", defectQty: "2", severity: "高",
        //   fileInfo: [
        //     { fileId: "20230719_451223134738060933.docx", name: "WMS测试报告2.docx", url: "/api/file/Download?encryption=00B343A3CB5CD63953BC06B264F5532C5747B1F8A932A873F66F0D11CA8CA33D3A008829A6083D4A1AD4BE0A37D1FE8BD3B9E811CEBE348266F4477630C5D2DE" },
        //     { fileId: "20230719_451223134738060934.docx", name: "WMS测试报告2.docx", url: "/api/file/Download?encryption=00B343A3CB5CD63953BC06B264F5532C5747B1F8A932A873F66F0D11CA8CA33D3A008829A6083D4A1AD4BE0A37D1FE8BD3B9E811CEBE348266F4477630C5D2DE" },
        //     { fileId: "20230719_451223134738060935.docx", name: "WMS测试报告2.docx", url: "/api/file/Download?encryption=00B343A3CB5CD63953BC06B264F5532C5747B1F8A932A873F66F0D11CA8CA33D3A008829A6083D4A1AD4BE0A37D1FE8BD3B9E811CEBE348266F4477630C5D2DE" }
        //   ],
        //   userId: "王雄", creatorTime: "2023-07-27 11:15:00"
        // },
        // { defect: "2", defectName: "外观严重破损", defectQty: "3", severity: "中", fileInfo: [], userId: "王雄", creatorTime: "2023-07-27 11:15:00" },
        // { defect: "2", defectName: "外观严重破损", defectQty: "1", severity: "低", fileInfo: [], userId: "王雄", creatorTime: "2023-07-27 11:15:00" },
      ],
      dataForm: {
        inspectionItem: "",
        number: 0,
        defect: "",
        defectName: "",
        defectQty: 1,
        severity: "",
        fileInfo: []
      },
      defectBaseData: [
        // { id: "1", fullName: "外观严重破损", severity: "高" },
        // { id: "2", fullName: "外观严重破损", severity: "中" },
        // { id: "3", fullName: "外观严重破损", severity: "低" },
      ],
      rules: {
        defect: [
          { required: true, message: '请选择缺陷', trigger: '"blur"' },
        ],
        defectQty: [
          { required: true, message: '请输入数量', trigger: '"blur"' },
          { pattern: /^\d+$/, message: '只能输入正整数', trigger: 'blur' }
        ]
      },
      readOnly: false
    }
  },
  methods: {
    init(id, index, readOnly) {
      this.readOnly = readOnly
      this.dataForm.inspectionItem = id || 0
      this.dataForm.number = index || 0
      this.visible = true
      this.isRefresh = false;
      this.$nextTick(() => {
        this.getList()
        this.getDefectBaseData()
      })
    },
    reset() {
      this.dataForm = {
        inspectionItem: "",
        number: 0,
        defect: "",
        defectName: "",
        defectQty: 1,
        severity: "",
        fileInfo: []
      }
    },
    getDefectBaseData() {
      this.$store.dispatch('qms/getDefactData').then((res) => {
        this.defectBaseData = res
      })
    },
    getList() {
      let query = {
        inspectionItem: this.dataForm.inspectionItem,
        number: this.dataForm.number
      }
      getIpqcInspectionitemdefectList(query).then(res => {
        this.defectList = res.data.list
      })
    },
    handleClick(file) {
      // 点击下载文件
      if (!file.fileId) return
      getDownloadUrl('annex', file.fileId).then(res => {
        this.shzy.downloadFile(res.data.url)
      })
    },
    handleDel(index, id) {
      this.$confirm(this.$t('common.delTip'), this.$t('common.tipTitle'), {
        type: 'warning'
      }).then(() => {
        delIpqcInspectionitemdefect(id).then(res => {
          this.isRefresh = true
          this.$message({
            type: 'success',
            message: res.msg,
            onClose: () => {
              this.defectList.splice(index, 1);
            }
          });
        })
        this.$message({ type: 'success', message: res.msg });
      }).catch(() => { });
    },
    dataFormSubmit() {
      this.$refs['elForm'].validate((valid) => {
        if (valid) {
          let defectInfo = this.defectBaseData.find(x => x.id == this.dataForm.defect)
          this.dataForm.defectName = defectInfo.name;
          this.dataForm.severity = defectInfo.severity;
          this.btnLoading = true;
          createIpqcInspectionitemdefect(this.dataForm).then((res) => {
            this.btnLoading = false;
            this.isRefresh = true;
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.getList()
                this.reset()
              }
            })
          }).catch((res) => {
            this.btnLoading = false;
          })
        }
      })
    },
    handleClose() {
      if (this.isRefresh) {
        this.$emit('refresh')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.timeline {
  padding-top: 30px;
}

>>>.el-timeline-content {
  p {
    padding: 0;
    margin: 0;
  }

  .text {
    line-height: 20px;
    margin-bottom: 10px;
    color: #475059;
    font-size: 14px;
  }

  .time {
    font-size: 12px;
    display: flex;
    justify-content: space-between;
    opacity: 0.6;

    .del {
      display: none;
      color: rgb(255, 91, 91);
      cursor: pointer;

      i {
        margin-right: 5px;
      }
    }
  }

  &:hover {
    .del {
      display: block;
    }
  }
}
</style>